﻿using System;
using System.Data;
using System.Data.SqlServerCe;
using System.Configuration;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.ComponentModel;

namespace Randomizer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        String _ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;
        BindingList<GeneratedItems> itemsList = new BindingList<GeneratedItems>();
        public AddForm add;
        

        public MainWindow()
        {
            InitializeComponent();
            BindCustomGrid(); 
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Select random item from enabled datatables
            Generate();
        }


        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void dataGrid5_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        public void BindCustomGrid()
        {
            //set connection object
            SqlCeConnection _Conn = new SqlCeConnection(_ConnectionString);
            
            //open DB conn
            _Conn.Open();

            SqlCeDataAdapter _Adapter = new SqlCeDataAdapter("SELECT * FROM Core_Table", _Conn);

            DataSet _Bind = new DataSet();
            _Adapter.Fill(_Bind, "CustomData");

            CustomList.DataContext = _Bind;

            _Conn.Close();
        }

        public int Randomize(int max)
        {
            Random random = new Random();
            int result = random.Next(0, max);
            return result;
        }

        public void Generate()
        {
            int max, randomNum;
            string[] result = new string[5];
            string query;

            SqlCeConnection _Conn = new SqlCeConnection(_ConnectionString);
            SqlCeDataReader reader;
            SqlCeCommand comm;
            
            //get max number of entries in table
            max = getMaxEntries("Core_Table");

            //generate random number
            Random random = new Random();

            randomNum = random.Next(1, max + 1);
            query = "SELECT * FROM Core_Table WHERE ID = "+randomNum;
            comm = new SqlCeCommand(query, _Conn);

            //open DB conn
            _Conn.Open();
            reader = comm.ExecuteReader();

            reader.Read();
            for(int i = 0;i<4;i++)
            {
                result[i] = reader.GetString(i);
            }
  
            itemsList.Add(new GeneratedItems(result[0], result[1], result[2], result[3]));

            RandomList.DataContext = itemsList;

            _Conn.Close();

            RandomList.ScrollIntoView(RandomList.Items[RandomList.Items.Count-1]);
        }

        private void dataGrid5_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private int getMaxEntries(string table)
        {
            int num;

            SqlCeConnection _Conn = new SqlCeConnection(_ConnectionString);

            //Randomly Choose a number based on latest entry
            string idQuery = "SELECT TOP 1 ID FROM " + table + " ORDER BY ID DESC";

            SqlCeCommand rComm = new SqlCeCommand(idQuery, _Conn);

            _Conn.Open();

            num = Convert.ToInt32(rComm.ExecuteScalar());

            _Conn.Close();

            return num;
        }

        private void NewItem_Click(object sender, RoutedEventArgs e)
        {
            add = new AddForm(this);
            add.Show();
        }

        private void DeleteItem_Click(object sender, RoutedEventArgs e)
        {
            DataRowView row = (DataRowView)CustomList.SelectedItem;

            String selName = row["Name"].ToString();

            SqlCeConnection _Conn = new SqlCeConnection(_ConnectionString);

            string delQuery = "DELETE FROM Core_Table WHERE Name = '" + selName + "'";

            SqlCeCommand comm = new SqlCeCommand(delQuery,_Conn);

            _Conn.Open();

            comm.ExecuteNonQuery();

            _Conn.Close();

            BindCustomGrid();    
        }

        private void EditItem_Click(object sender, RoutedEventArgs e)
        {
            DataRowView row = (DataRowView)CustomList.SelectedItem;

            String itemName = row["Name"].ToString();

            EditForm edit = new EditForm(this,itemName);

            edit.Show();
        }

        private void UC_Checked(object sender, RoutedEventArgs e)
        {
              
        }


       
    }
}
